/*
 * Copyright (c) 2012 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.example.helloandroidworld;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Asynchronously load the tasks.
 * 
 * @author Yaniv Inbar
 */
class AsyncLoadTasks extends CommonAsyncTask {

	
  AsyncLoadTasks(MainActivity tasksSample) {
    super(tasksSample);
  }

  @Override
  public void doInBackground() throws IOException {
	
	  //gets rowid for selected Google Account
	  	int status = activity.status;
	  	String free = activity.available[status].toString();
	    String cred = activity.credential.getSelectedAccountName();
	    String rowid = client.query().sqlGet("SELECT ROWID FROM 16C72S42_fpovh3uKiozqEJDBuLM5c-nlNjaSRvk WHERE Username = '" +cred+ "'").execute().getRows().get(0).get(0).toString(); 
	    
	int temp = activity.myLocation.getMyLocation().getLatitudeE6();
	int temp2 = activity.myLocation.getMyLocation().getLongitudeE6();
	    	  
	//updates 123456
	client.query().sql("UPDATE 16C72S42_fpovh3uKiozqEJDBuLM5c-nlNjaSRvk SET Availability = '"+ free  +"' WHERE ROWID = '"+rowid+"'").execute();
    client.query().sql("UPDATE 16C72S42_fpovh3uKiozqEJDBuLM5c-nlNjaSRvk SET Location = '"+temp+","+temp2+"' WHERE ROWID = '"+rowid+"'").execute();
	
	
	List<String> result = new ArrayList<String>();
	List<String> aresult = new ArrayList<String>();
	List<String> nresult = new ArrayList<String>();
	List<String> noresult = new ArrayList<String>();
	

		//gets coordinates to display
	List<List<Object>> locations =
    client.query().sqlGet("SELECT Location FROM 16C72S42_fpovh3uKiozqEJDBuLM5c-nlNjaSRvk WHERE Username NOT EQUAL TO '" +cred+ "'").execute().getRows();
    
		//gets names to display
	List<List<Object>> names =
	   client.query().sqlGet("SELECT Name FROM 16C72S42_fpovh3uKiozqEJDBuLM5c-nlNjaSRvk WHERE Username NOT EQUAL TO '" +cred+ "'").execute().getRows();
	
	
			//gets availability to display
    List<List<Object>> avail = 
    client.query().sqlGet("SELECT Availability FROM 16C72S42_fpovh3uKiozqEJDBuLM5c-nlNjaSRvk WHERE Username NOT EQUAL TO '" +cred+ "'").execute().getRows();
    
	//gets numbers to display
    List<List<Object>>  nums =
    client.query().sqlGet("SELECT number FROM 16C72S42_fpovh3uKiozqEJDBuLM5c-nlNjaSRvk WHERE Username NOT EQUAL TO '" +cred+ "'").execute().getRows();
    
    
    
     for (int i=0;  i < (locations.size()); i++) 
      {
        result.add(locations.get(i).get(0).toString());
        aresult.add(avail.get(i).get(0).toString());
        nresult.add(names.get(i).get(0).toString());
        noresult.add(nums.get(i).get(0).toString());
      }

    activity.locationsList = result;
    activity.availableList = aresult;
    activity.nameList = nresult;
    activity.numbersList = noresult;
    
  }

  static void run(MainActivity tasksSample) {
    new AsyncLoadTasks(tasksSample).execute();
  }
}
